<template>
  <div>
    <!--      页面工具栏-->
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible=true">新增菜单</el-button>
      </el-form-item>
    </el-form>
    <!--      表格-->
    <el-table :data="tableData" border row-key="id" default-expand-all
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}" style="width: 100%"
              :cell-style="{padding:'2px'}">
      <el-table-column label="名称" width="180" prop="name">
      </el-table-column>
      <el-table-column label="权限编码" width="180" prop="perms">
      </el-table-column>
      <el-table-column label="菜单图标" width="180" prop="icon">
      </el-table-column>
      <el-table-column label="菜单类型" width="120" prop="type">
        <template slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.type===2 " type="warning">按钮</el-tag>
          <el-tag size="mini" v-else-if="scope.row.type===1 " type="success">菜单</el-tag>
          <el-tag size="mini" v-else-if="scope.row.type===0 ">目录</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="菜单URL" prop="path">
      </el-table-column>
      <el-table-column label="菜单组件" prop="component">
      </el-table-column>
      <el-table-column label="状态" prop="statu">
        <template slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.statu===1 " type="success">正常</el-tag>
          <el-tag size="mini" v-else-if="scope.row.statu===0 " type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="340">
        <template slot-scope="scope">
          <el-button type="text" @click="editMenu(scope.row.id)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确认要删除吗！！！！" @confirm="deleteMenu(scope.row.id)">
            <el-button slot="reference" type="text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新建菜单" :visible.sync="dialogFormVisible" width="550px" :before-close="handleClose">
      <el-form :model="MenuForm" ref="MenuForm" label-width="100px" :rules="rules">
        <el-form-item label="上级菜单" prop="parentId">
          <el-select v-model="MenuForm.parentId" placeholder="请选择上级菜单">
            <template v-for="(item,index) in tableData">
              <el-option
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
              <template v-for="(childMenu,i) in item.children">
                <el-option
                    :key="childMenu.id"
                    :label="childMenu.name"
                    :value="childMenu.id">
                <span style="padding-left: 20px">
                  {{ ' -' + childMenu.name }}
                </span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name" label-width="100px">
          <el-input v-model="MenuForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="perms" label-width="100px">
          <el-input v-model="MenuForm.perms" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon" label-width="100px">
          <el-input v-model="MenuForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单URL" prop="path" label-width="100px">
          <el-input v-model="MenuForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单组件" prop="component" label-width="100px">
          <el-input v-model="MenuForm.component" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单类型" prop="type" label-width="100px">
          <el-radio-group v-model="MenuForm.type">
            <el-radio :label="3">目录</el-radio>
            <el-radio :label="6">菜单</el-radio>
            <el-radio :label="9">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单状态" label-width="100px" prop="statu">
          <el-radio-group v-model="MenuForm.statu">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('RoleForm')">保存用户</el-button>
        <el-button type="primary" @click="resetForm('RoleForm')">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      MenuForm: {},
      dialogFormVisible: false,
      tableData: [],
      rules: {
        parentId: [
          {required: true, message: '请选择上级菜单', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入菜单名称', trigger: 'blur'}
        ],
        perms: [
          {required: true, message: '请输入权限编码', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择菜单类型', trigger: 'blur'}
        ],
        statu: [
          {required: true, message: '请选择菜单状态', trigger: 'blur'},
        ],
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    submitForm(formName){
      this.$refs.MenuForm.validate(valid => {
        if (valid) {
          this.$axios.post('/system/menu/' + (this.MenuForm.id ? 'update' : 'save'), this.MenuForm).then(response => {
            this.$message({
              message: response.data.message,
              type: 'success',
              // showClose: true,
              duration:1000,
              onClose: () => {
                this.loadMenuList()
                this.dialogFormVisible = false
                this.resetForm(formName)
              }
            })
          })
        }
      })
    },
    resetForm() {
      this.MenuForm = {}
      this.$refs.MenuForm.resetFields();
    },
    handleClose() {
      this.MenuForm = {}
      this.dialogFormVisible = false
    },
    editMenu(id) {
      this.$axios('/menu/userInfo/' + id).then(response => {
        this.MenuForm = response.data.data
        this.dialogFormVisible = true
      })
    },
    deleteMenu(id) {
      let ids = [];
      id ? ids.push(id) : this.selection.forEach(u => {
        ids.push(u.id)
      })
      this.$axios.post('/system/role/delete', ids).then(response => {
        this.$message({
          message: response.data.message,
          type: 'success',
          showClose: true,
          onClose: () => {
            this.loadRoleList()
          }
        });
      })
    },
    loadMenuList() {
      this.$axios.get('/system/menu/list').then(response => {
        this.tableData = response.data.data
      });
    },
  },
  created() {
    this.loadMenuList()
  },
  mounted() {
    this.loadMenuList()
  }
};
</script>

<style scoped>
.el-table__body {
  font-size: 14px;
}

.el-button:hover {
  color: #333
}

.el-popconfirm-reference {
  cursor: pointer;
}

.el-pagination {
  display: flex;
  justify-content: flex-end;
}

.el-tag {
  font-size: 12px;
  padding: 2px 6px;
}

.el-table-column--selection {
  text-align: center;
}

.el-avatar {
  border-radius: 50%;
}

.el-divider {
  margin: 0 6px;
}

/*上传头像*/
/*.avatar-uploader {*/
/*  border: 1px dashed #d9d9d9;*/
/*  border-radius: 6px;*/
/*  cursor: pointer;*/
/*  position: relative;*/
/*  overflow: hidden;*/
/*}*/

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-dialog {
  text-align: center;
}

/* Style the dialog header */
.el-dialog__header {
  background-color: #2f4050;
  color: #fff;
  border-radius: 4px 4px 0 0;
  border-bottom: none;
}

/* Style the dialog title */
.el-dialog__title {
  font-size: 22px;
  font-weight: bold;
  padding: 20px;
}

/* Style the form labels */
.el-form-item__label {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

/* Style the input fields */
.el-input__inner {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  color: #606266;
}

/* Style the radio buttons */


.el-radio-button {
  flex: 1;
}

.dialog-footer {
  padding: 20px;
  text-align: right;
  border-top: 1px solid #ebeef5;
  border-radius: 0 0 4px 4px;
}

.dialog-footer .el-button {
  margin-left: 10px;
  border-radius: 4px;
}

.dialog-footer .el-button--primary {
  background-color: #409EFF;
  border-color: #409EFF;
}

.dialog-footer .el-button--primary:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.dialog-footer .el-button--primary:active {
  background-color: #3a8ee6;
  border-color: #3a8ee6;
}

.dialog-footer .el-button--default {
  color: #333;
  border-color: #dcdfe6;
}

.dialog-footer .el-button--default:hover {
  color: #409EFF;
  border-color: #409EFF;
}
</style>